package com.tom.utils;

import cn.hutool.json.JSONUtil;
import com.tom.enums.HttpStatusInfo;

import java.util.HashMap;
import java.util.Map;

public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	public R() {
		put("code", HttpStatusInfo.SUCCESS_200.getCode());
		put("msg", HttpStatusInfo.SUCCESS_200.getMsg());
		put("data",null);
	}

	public R(Object data){
		put("code", HttpStatusInfo.SUCCESS_200.getCode());
		put("msg", HttpStatusInfo.SUCCESS_200.getMsg());
		put("data",data);
	}

	public static R error() {
		return error(HttpStatusInfo.ERROR_500.getCode(), HttpStatusInfo.ERROR_500.getMsg());
	}

	public static R error(String msg) {
		return error(HttpStatusInfo.ERROR_500.getCode(), msg);
	}

	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("msg", msg);
		return r;
	}

	public static R ok(Map<String, Object> map) {
		return new R(map);
	}

	public static R ok() {
		return new R();
	}

	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<>();
		map.put("name","tom");
		map.put("age",18);
		map.put("specialized","系统架构师");
		map.put("sex","男")	;
		System.out.println(JSONUtil.parse(R.ok(map)).toStringPretty());

		System.out.println("---------------------------------------");
		System.out.println(JSONUtil.parse(R.ok()).toStringPretty());
	}
}